package com.gec.service.Impl;

import com.gec.dao.ProcessConfigDao;
import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.domain.ProcessConfig;
import com.gec.service.abst.WorkFlowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

@Service
public class WorkflowServiceImpl implements WorkFlowService {

    private final RepositoryService repService;

    private final ProcessConfigDao configDao;

    public WorkflowServiceImpl(RepositoryService repService, ProcessConfigDao configDao) {
        this.repService = repService;
        this.configDao = configDao;
    }

    /*
        String note;            [表单中包含]
        String procCategory;    [表单中包含]
        String deploymentId;     [布署后即可得到]
    */
    @Override
    public String addDeployment(
            ProcessConfig config, InputStream is) {
        //{1}创建一个压缩流..
        ZipInputStream zipIS = new ZipInputStream(is);
        //{2}将 zip 档部署到 工作表中。
        Deployment deploy = repService.createDeployment()
                .name(config.getNote())
                .addZipInputStream(zipIS)
                .deploy();
        //{2}获取布署 ID
        String deployID = deploy.getId();
        //{3}设置 布署 ID 到 config 中
        config.setDeploymentId(deployID);
        //{4}做一个流程定义信息查询 [把布署到工作表中流程信息填入 config]
        ProcessDefinition proDef = repService
                .createProcessDefinitionQuery()
                .deploymentId(deployID)
                .singleResult();
        //{5}设置以下数据到 config
        String defID = proDef.getId();
        config.setId(defID);  //流程定义ID
        config.setProcDefId(defID);         //procDefId
        config.setProcKey(proDef.getKey()); //procKey
        config.setVersion(proDef.getVersion()); //version
        //{5}我自己另存一份流程定义数据。
        configDao.addProcessConfig(config);
        //{6}返回一个流程布署 ID.
        return deployID;
    }

    @Override
    public ProcessConfig getProcessByCategory(String category) {
        ProcessConfig processConfig = configDao.getProcessByCategory(category);
        return processConfig;
    }

    @Override
    public PageBean<ProcessConfig> getProcessList(PageParam param) {
        Map<String, String> map = param.getKeywords(); //拿到搜索关键字
        List<ProcessConfig> list;
        PageBean<ProcessConfig> pBean = new PageBean<>();
        int page = param.getPage();
        int size = param.getLimit();
        PageHelper.startPage(page, size, true);
        list = configDao.getProcessConfigs(map);
        PageInfo<ProcessConfig> pageInfo = new PageInfo<>(list);
        int count = (int) pageInfo.getTotal();
        System.out.println(list);
        pBean.setCount(count);
        pBean.setData(list);
        pBean.setCode(0);
        return pBean;
    }

    @Override
    public ProcessConfig getProcessConfigById(String id)
    {
        return configDao.getProcessConfigById(id);
    }

    @Override
    public boolean delProcessConfigById(String id)
    {
        ProcessConfig config = configDao.getProcessConfigById(id);
        String deploymentId = config.getDeploymentId();
        repService.deleteDeployment(deploymentId);
        return configDao.delProcessConfigById(id) > 0;
    }
}
